<?php
session_start();
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Util/Tool.php';
// set timeout period in seconds
$inactive = 500800;
// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) )
{
	$session_life = time() - $_SESSION['timeout'];
	if($session_life > $inactive)
	{
		session_destroy();
		$gotoPage = Tool::curPageURL();
		$_SESSION['gotoPage'] = $gotoPage;
		header("Location: index.php?type=login&action=logout");
	}
}
$_SESSION['timeout'] = time();
$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__.DIRECTORY_SEPARATOR . '../view'.DIRECTORY_SEPARATOR.'templates');
$smarty->setCompileDir(__DIR__.DIRECTORY_SEPARATOR . '../view'.DIRECTORY_SEPARATOR.'templates_c');
$templateDir = $smarty->getTemplateDir(0);
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Context/BaseContext.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/UserService.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/ServiceResult/ServiceResult.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Entity/Info/Schedule.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/ScheduleService.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../_config/setting/SMTPConfig.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Util/Setting.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Util/Pager.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../_config/setting/global.setting.php';
if (!file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.'../_config/setting/setting.ini'))
{
	echo "Configuration file not found!";
}
else
{
	// include class
	require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../plugin/Config/Config.php';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
$templateDir = $smarty->getTemplateDir(0) ;
$smarty->assign('templateDir',$glb["websiteURL"]."/view/templates");
$smarty->assign('websiteURL',$glb["websiteURL"]);
$smarty->assign('get',$_GET);
$smarty->display($templateDir . "/temp1/head.tpl");

$context = new BaseContext();
$result = new ServiceResult();
?>
</head>
<body>
<?php
if(!isset($_GET['func']))
{
	header("Location:index.php?func=dash");
}
if(isset($_GET['func']) && $_GET['func']=="login")
{
	if(isset($_SESSION['user']))
	{
		header("Location:index.php?func=dash");
	}
	else
	$smarty->display($templateDir . "/temp1/login.tpl");
}
else
{
?>
	<?php
		if(!isset($_SESSION['user']))
		{
			$gotoPage = Tool::curPageURL();
			$_SESSION['gotoPage'] = $gotoPage;
			header("Location:index.php?func=login");
			die();
		}
		
		if(isset($_SESSION['user']))
		{
			$oid = $_SESSION['user']['ID'];
		}
		$userService = new UserService();
		
		$user = $userService->findByOid($context, $result, $oid);
		
		$smarty->assign("user",$user);
		$smarty->assign("SESSION",$_SESSION);
		$smarty->display($templateDir . "/temp1/header.tpl");
	?>
		<!-- Main Wrapper -->
	    <div id="mws-wrapper">
	    	<!-- Necessary markup, do not remove -->
			<div id="mws-sidebar-stitch"></div>
			<div id="mws-sidebar-bg"></div>
	        
	        <!-- Sidebar Wrapper -->
	        <div id="mws-sidebar">
	            <!-- Main Navigation -->
	            <?php 
	           		$smarty->display($templateDir . "/temp1/navigation.tpl");
	            ?>
	            <!-- End Navigation -->
	            
	        </div>
	        
	        
	        <!-- Container Wrapper -->
	        <div id="mws-container" class="clearfix">
	        
	        	<!-- Main Container -->
	            <div class="container">	                
	               
	            	<?php 
						if(isset($_GET['func']))
						{
							$func = $_GET['func'];
							if($func == "dash")
							{
								$scheduleService = new ScheduleService();
								$tool = new Tool();										
								$scheduleArr = $scheduleService->getNewSchedule($context, $result);
								$smarty->assign('tool',$tool);
								$smarty->assign('scheduleArr',$scheduleArr);
								$smarty->display($templateDir . "/temp1/listSchedule.tpl");
							}
							if($func == "website")
							{
								if(isset($_GET['opt']))
								{
									$opt = $_GET['opt'];
									if($opt == "hostConfig")
									{
										$c = new Config();
										$root =& $c->parseConfig(dirname(__FILE__).DIRECTORY_SEPARATOR.'../_config/setting/setting.ini', 'inifile');
										$settingsSection =& $root->getItem("section", "delivery-collection-schedule");
										$host =& $settingsSection->getItem("directive", "delivery_collection_schedule_MYSQL_HOST");
										$dbName =& $settingsSection->getItem("directive", "delivery_collection_schedule_MYSQL_NAME");
										$user =& $settingsSection->getItem("directive", "delivery_collection_schedule_MYSQL_USER");
										$pass =& $settingsSection->getItem("directive", "delivery_collection_schedule_MYSQL_PASS");
										 
										$host = $host->getContent();
										$dbName = $dbName->getContent();
										$user = $user->getContent();
										$pass = $pass->getContent();
										 
										$smarty->assign('host', Tool::decode($host, "sTunnInVietnAm@2012#"));
										$smarty->assign('dbName', Tool::decode($dbName, "sTunnInVietnAm@2012#"));
										$smarty->assign('user', Tool::decode($user, "sTunnInVietnAm@2012#"));
										$smarty->assign('pass', Tool::decode($pass, "sTunnInVietnAm@2012#"));
										$smarty->display($templateDir . "/temp1/configHost.tpl");
									}
									
									if($opt == "globalConfig")
									{
										$smarty->assign('websiteURL', $glb["websiteURL"]);
										$smarty->assign('settingDir', $glb["settingDir"]);
										$smarty->assign('layoutDir', $glb["layoutDir"]);
										$smarty->assign('controllerDir', $glb["controllerDir"]);
										$smarty->assign('contactEmail', $glb["contactEmail"]);
										$smarty->display($templateDir . "/temp1/configGlobal.tpl");
									}
								
									if($opt == "emailConfig")
									{
										$smarty->assign('hostEmail', Tool::decode($smtp["host"],"sTunnInVietnAm@2012#"));
										$smarty->assign('port', Tool::decode($smtp["port"],"sTunnInVietnAm@2012#"));
										$smarty->assign('userHost', Tool::decode($smtp["user"],"sTunnInVietnAm@2012#"));
										$smarty->assign('passHost', Tool::decode($smtp["pass"],"sTunnInVietnAm@2012#"));
										$smarty->display($templateDir . "/temp1/configEmail.tpl");
									}
								}
							}
							if($func == "managerment")
							{
								if(isset($_GET['opt']))
								{
									$opt = $_GET['opt'];
									if($opt == "input")
									{
										$tool = new Tool();	
										$date = date('d-m-Y');
										$time = date('H:i');
										$smarty->assign('tool',$tool);
										$smarty->assign('date',$date);
										$smarty->assign('time',$time);
										$smarty->display($templateDir . "/temp1/input.tpl");
									}
								}
							}
							if($func == "schedule")
							{
								$scheduleService = new ScheduleService();
								
								if(isset($_GET['opt']))
								{
									$opt = $_GET['opt'];
									if($opt == "listNewSchedule")
									{
										$tool = new Tool();										
										$scheduleArr = $scheduleService->getNewSchedule($context, $result);
										$smarty->assign('tool',$tool);
										$smarty->assign('scheduleArr',$scheduleArr);
										$smarty->display($templateDir . "/temp1/listSchedule.tpl");
									}
									if($opt == "listFinishSchedule")
									{
										$tool = new Tool();										
										$scheduleArr = $scheduleService->getFinishedSchedule($context, $result, 2);
										$smarty->assign('tool',$tool);
										$smarty->assign('scheduleArr',$scheduleArr);
										$smarty->display($templateDir . "/temp1/listSchedule.tpl");
									}
									if($opt == "listAlertDelivery")
									{
										$tool = new Tool();	
										$dateNow = date('Y-m-d');
										$timeNow = date('H:i');									
										$scheduleArr = $scheduleService->getScheduleByStatusByDeliveryDate($context, $result, 0, $dateNow, $timeNow, 0, 1000);
										$smarty->assign('tool',$tool);
										$smarty->assign('scheduleArr',$scheduleArr);
										$smarty->display($templateDir . "/temp1/listAlertDelivery.tpl");
									}
									if($opt == "listAlertCollection")
									{
										$tool = new Tool();	
										$dateNow = date('Y-m-d');
										$timeNow = date('H:i');									
										$scheduleArr = $scheduleService->getScheduleByStatusByCollectionDate($context, $result, 1, $dateNow, $timeNow, 0, 1000);
										$smarty->assign('tool',$tool);
										$smarty->assign('scheduleArr',$scheduleArr);
										$smarty->display($templateDir . "/temp1/listAlertCollection.tpl");
									}
									if($opt == "listDelivery")
									{
										$tool = new Tool();	
										$dateNow = date('Y-m-d');
										$timeNow = date('H:i');									
										$scheduleArr = $scheduleService->getScheduleByStatus($context, $result, 0);
										$smarty->assign('tool',$tool);
										$smarty->assign('scheduleArr',$scheduleArr);
										$smarty->display($templateDir . "/temp1/listAlertDelivery.tpl");
									}
									if($opt == "listCollection")
									{
										$tool = new Tool();	
										$dateNow = date('Y-m-d');
										$timeNow = date('H:i');									
										$scheduleArr = $scheduleService->getScheduleByStatus($context, $result, 1);
										$smarty->assign('tool',$tool);
										$smarty->assign('scheduleArr',$scheduleArr);
										$smarty->display($templateDir . "/temp1/listAlertCollection.tpl");
									}
									if($opt == "viewSchedule")
									{										
										if(isset($_GET['scheduleID']))
										{
											$tool = new Tool();																				
											$smarty->assign('tool',$tool);
											$schedule = $scheduleService->getScheduleByOid($context, $result, $_GET['scheduleID']);
											
											$smarty->assign('schedule',$schedule);
											$smarty->display($templateDir . "/temp1/viewSchedule.tpl");
										}
									}
									if($opt == "viewFinishedSchedule")
									{										
										if(isset($_GET['scheduleID']))
										{
											$tool = new Tool();																				
											$smarty->assign('tool',$tool);
											$schedule = $scheduleService->getScheduleByOid($context, $result, $_GET['scheduleID']);
											
											$smarty->assign('schedule',$schedule);
											$smarty->display($templateDir . "/temp1/viewFinishedSchedule.tpl");
										}
									}
									if($opt == "viewCollectionSchedule")
									{										
										if(isset($_GET['scheduleID']))
										{
											$tool = new Tool();																				
											$smarty->assign('tool',$tool);
											$schedule = $scheduleService->getScheduleByOid($context, $result, $_GET['scheduleID']);
											
											$smarty->assign('schedule',$schedule);
											$smarty->display($templateDir . "/temp1/viewCollectionSchedule.tpl");
										}
									}
									if($opt == "viewDeliverySchedule")
									{										
										if(isset($_GET['scheduleID']))
										{
											$tool = new Tool();																				
											$smarty->assign('tool',$tool);
											$schedule = $scheduleService->getScheduleByOid($context, $result, $_GET['scheduleID']);
											
											$smarty->assign('schedule',$schedule);
											$smarty->display($templateDir . "/temp1/viewDeliverySchedule.tpl");
										}
									}
									
								}
								
							}
						}
	            	?>
	                
	            </div>
	            <!-- End Main Container -->
	            
	            <!-- Footer -->
	            <div id="mws-footer">
	            	Copyright Your Website 2012. All Rights Reserved.
	            </div>
	            <!-- End Footer -->
	            
	        </div>
	        <!-- End Container Wrapper -->
	        
	    </div>
	    <!-- End Main Wrapper -->
	
<?php 
}
?>
</body>
</html>